#include "mq_connection.hpp"

int main()
{
    // 1.实例化异步工作线程对象
    mq::AsyncWorker::ptr awp = std::make_shared<mq::AsyncWorker>();
    
    // 2.实例化Connection对象
    mq::Connection::ptr conn = std::make_shared<mq::Connection>("127.0.0.1", 8085, awp);
    // 3.构建信道
    
    mq::Channel::ptr channel = conn->openChannel();
    
    // 4.通过信道完成服务
    //  1.声明交换机
    google::protobuf::Map<std::string, std::string> tmp_map;
    
    channel->declareExchange("exchange1", mq::ExchangeType::TOPIC, true, false, tmp_map);
    
    //  2.声明队列
    channel->declareQueue("queue1", true, false, false, tmp_map);
    channel->declareQueue("queue2", true, false, false, tmp_map);
    

    //  3.队列和交换机绑定
    // 绑定queue1-exchange1, 且binding_key设置为queue1
    channel->queueBind("exchange1", "queue1", "queue1");
    // 绑定queue2-exchange1, 且binding_key设置为news.music.#
    channel->queueBind("exchange1", "queue2", "news.music.#");
    
    // 5.发布消息 
    // queue2可以收到
    for(int i = 0; i < 10; i++)
    {
        mq::BasicProperties bp;
        bp.set_id(mq::UUIDHelper::uuid());
        bp.set_delivery_mode(mq::DeliveryMode::DURABLE);
        // bp.set_routing_key("queue1");
        bp.set_routing_key("news.music.pop");
        channel->basicPublish("exchange1", &bp, "Hello World-" + std::to_string(i));
    }

    // 匹配成功queue2
    mq::BasicProperties bp;
    bp.set_id(mq::UUIDHelper::uuid());
    bp.set_delivery_mode(mq::DeliveryMode::DURABLE);
    bp.set_routing_key("news.music.sport"); 
    channel->basicPublish("exchange1", &bp, "hahahahahaha -");

    // 匹配失败
    bp.set_routing_key("news.sport");
    channel->basicPublish("exchange1", &bp, "hehehehehehehe -");

    // 6.关闭信道
    conn->closeChannel(channel);
    return 0;
}